﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.BLL;
using WebApp.BLL.Manage;
using WebApp.Models;
using WebApp.ViewModels;
using WebApp.Web.Core;
using WebApp.Web.Core.Auth;

namespace kzjd.rycp.WebApp.Areas.Basic.Controllers
{
    public class OfficeDeptController : Controller
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger("WebApp.Log");

        #region List

        [AuthorizeFilter("Basic.OfficeDept")]
        public ActionResult List()
        {
            //编辑权限
            ViewBag.CanEdit = UserAuthBLL.HasAuth("Basic.OfficeDept");

            return View();
        }

        #endregion

        #region 查询列表数据

        [AuthorizeFilter("Basic.OfficeDept")]
        public JsonResult DeptListJson(int page, int rows, string s, string sort, string order)
        {
            var deptservice = new S_departmentService();
            Dictionary<string, object> result = new Dictionary<string, object>();

            int rowscount = 0;

            string depttype = enmDeptType.Manager.ToString();

            var list = deptservice.PageList(page, rows, depttype, s, sort, order, out rowscount).Select(p => new
            {
                p.Id,
                p.Sort,
                p.Depttype,
                p.Deptcode,
                p.Deptname,
                p.Remark,
                Updatetime = CommonStr.GetDateTimeStr(p.Updatetime)
            });

            result.Add("total", rowscount);
            result.Add("rows", list);

            return Json(result);
        }

        #endregion

        #region 删除

        [AuthorizeFilter("Basic.OfficeDept")]
        public JsonResult Delete(string ids)
        {
            try
            {
                new S_departmentService().DeleteByIds(ids);

                return Json(new AjaxResult("0", "删除成功！"));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return Json(new AjaxResult(AjaxResult.RESULT_ERROR, ex.Message));
            }
        }

        #endregion

        #region 保存

        [AuthorizeFilter("Basic.OfficeDept")]
        public JsonResult Save(string jsondata)
        {
            try
            {
                var deptservice = new S_departmentService();

                //序列化Json数据
                JsonDataModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonDataModel>(jsondata);

                var dept = new S_department();
                if (model.Id > 0)
                {
                    dept = deptservice.Find(model.Id);
                }
                //通过反射给属性赋值
                model.SetModelData<S_department>(dept, true);

                //保存
                deptservice.Save(dept);

                return Json(new AjaxResult(AjaxResult.RESULT_OK, "保存成功！"));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return Json(new AjaxResult(AjaxResult.RESULT_ERROR, ex.Message));
            }
        }



        #endregion

        #region 编辑/查看

        [AuthorizeFilter("Basic.OfficeDept")]
        // GET: Manage/Role/Edit/5
        public ActionResult Edit(int id)
        {
            if (id == 0)
            {
                return View(new S_department()
                {
                    Depttype = enmDeptType.Manager.ToString()
                });
            }
            else
            {

                var item = new S_departmentService().Find(id);

                if (item == null)
                {
                    throw new HttpException(404, "Department (id='" + id.ToString() + "') not found!");
                }

                return View(item);
            }
        }

        #endregion

        #region 重复验证

        [IgnoreAuthorizeFilter]
        public string ExsitCheck(int id, string deptname)
        {
            string depttype = enmDeptType.Manager.ToString();

            return new S_departmentService().Exist(id, depttype, deptname) ? "no" : "yes";
        }

        #endregion

    }
}